import re
disp_int = '''
<Layer3Switch-1>display interface
GigabitEthernet0/0/1 current state : UP
Line protocol current state : UP
Description:
Switch Port, PVID :    1, TPID : 8100(Hex), The Maximum Frame Length is 9216
IP Sending Frames' Format is PKTFMT_ETHNT_2, Hardware address is 4c1f-cc92-675c
Last physical up time   : 2021-10-25 15:22:35 UTC-08:00
Last physical down time : 2021-10-25 15:22:33 UTC-08:00
......'''
print(disp_int)
m1='Switch.+TPID.+Length.+'
print(re.search(m1,disp_int).group())
# . 表示匹配任意单个字符
#而且这种匹配模式是遇到回车就停止了
#重复符+或*则默认是尽可能多的多匹配，即所谓的“贪婪”模式

mac = '2, Hardware address is 4c1f-cc92-675c'
print(re.search(r'^\d+',mac).group())
#使用^来截取从开头到特定符号（包括特定符号）
mac = '23456, Hardware address is 4c1f-cc92-675c'
print(re.search(r'^\d+',mac).group())

prompt = '<Layer3Switch-1>display interface'
print(re.search(r'^.+>',prompt).group())
#使用了^.+> 取出了从开头到>的这一部分


line = 'Hardware address is 4c1f-cc92-675c'
print(re.search(r'\S+$',line).group())
#使用了\S代表了非空字符串，再加上$代表从行末往前
#共同组合取出了mac地址

#用[]表示列表
line = 'Hardware address is 4c1f-cc92-675c'
print(re.search(r'[Hh]ard[Ww]are',line).group())


commands = ['<Layer3Switch-1>display interface','[Layer3Switch-2]display interface']
for command in commands:
	print(command)



commands = ['<Layer3Switch-1>display interface','[Layer3Switch-2]display interface']
for command in commands:
	match=re.search(r'^.+[>\]]',command)
	if match:
		print(match.group())


line = 'Hardware address is 4c1f-cc92-675c'
print(re.search(r'[0-9a-zA-Z]+-[0-9a-zA-Z]+-[0-9a-zA-Z]+',line).group())

print(re.search(r'[^0-9]',line).group())
#[]和^字符一起使用，表示不要匹配[]中的字符

print(re.search(r'[^0-9]+',line).group())


#print(re.search(r'[0-9a-zA-Z]+-[0-9a-zA-Z]+-[0-9a-zA-Z]+',line).group())



#字符|表示前后两个表达式是或的关系
print(re.search(r'Hardware|address',line).group())

line = 'Hardware address is 4c1f-cc92-675c'
print(re.search(r'[0-9a-zA-Z]+-[0-9a-zA-Z]+-[0-9a-zA-Z]+',line).group())
print(re.search(r'([0-9a-zA-Z]+-)+[0-9a-zA-Z]+',line).group())

#贪婪限定
#*+?都是贪婪的，即能匹配多，就不匹配少，能匹配越多就匹配越多


line = '<text line> huawei text>'
match=re.search('<.*>',line)
print(match.group())

#如果要去掉重复符号的贪婪特性，可以在重复符号的后面加上一个问号？即可！
match=re.search('<.*?>',line)
print(match.group())


line = 'Hardware address is 4c1f-cc92-675c'
print(re.search(r'([0-9a-zA-Z]+-)+[0-9a-zA-Z]+',line).group())

print(re.search(r'([0-9a-zA-Z]+-)+?[0-9a-zA-Z]+?',line).group())

print(re.search(r'([0-9a-zA-Z]+-)+?[0-9a-zA-Z]+',line).group())

print(re.search(r'([0-9a-zA-Z]+-)+[0-9a-zA-Z]+?',line).group())



